import {DimensionTarget, Dimension} from '@/api/myPerformance'
import {CCard} from '@/components/card'
import PmTargetEnactDrawer from '@/components/drawer/pmTargetEnactDrawer'
import TargetLibImportDrawer from '@/components/drawer/targetLibImportDrawer'
import {CnNormalP, CnNormalSpan, CnBoldSpan} from '@/components/text'
import deepClone from '@/libs/deepClone'
import {PropType,reactive, toRefs, ref, defineComponent, watch, computed,onMounted} from 'vue'
import {unReplaceBreak} from '@/libs/toolkit'
import PhoneDimenBlock from './phone_setTarget_dimen_block'
import useModal from '@/hooks/modalHook'
import PhoneTargetBlock from './phone_setTarget_target_block'
import {PhoneScoreSelfPmData, PhoneScoreSelfPmTarget} from '..'

const ScoreDimen = defineComponent({
  name: 'ScoreDimen',
  props: {
    sourceType: {
      type: String as PropType<'self' | 'leader'>,
      required: true
    },

    pmId: {
      type: Number,
      required: true
    },

    hidden: {
      type: Boolean,
      default: true
    },
    owner: {
      type: Object as PropType<User.Info>,
      required: true
    },
    dimension: {
      type: Object as PropType<PhoneScoreSelfPmData>,
      required: true
    },
    showAppointScore: {
      type: Boolean,
      default: false
    },
    excludeUserIds: Array as PropType<string[]>
  },
  emits: {
    'update:dimension': (data: PhoneScoreSelfPmData): boolean => true,
    dimensionChange: (data: PhoneScoreSelfPmData): boolean => true
  },
  setup(props, {emit}) {
    const {
      visible: targetLibImportDrawerVisible,
      closeModal: closeTargetLibImportDrawerModal,
      openModal: openTargetLibImportDrawerModal
    } = useModal()
    const {
      visible: addTargetDrawerVisible,
      closeModal: closeAddTargetDrawerModal,
      openModal: openAddTargetDrawerModal
    } = useModal()
    const {
      visible: editTargetDrawerVisible,
      closeModal: closeEditTargetDrawerModal,
      openModal: openEditTargetDrawerModal
    } = useModal()

    let state = reactive<{
      targetLibImportDrawerVisible: boolean
      editTargetDrawerData: Evaluation.PmGroupDimensionTarget | undefined
      dimension: PhoneScoreSelfPmData | undefined
    }>({
      targetLibImportDrawerVisible: false,
      editTargetDrawerData: undefined,
      dimension: undefined
    })

    /**表格数据源 （排除掉了已删除的数据） */
    const tableDataSource = computed<PhoneScoreSelfPmTarget[]>(
      () => state.dimension?.pmTargetList || []
    )

    /**维度权重 */
    const dimensionWeightCount = computed(() => props.dimension?.pmDimensionWeight || 0)

    /**指标权重总和（不包含已删除的） */
    const targetWeightCount = computed(
      () => eval(tableDataSource.value.map(item => item.pmTargetWeight).join('+')) || 0
    )

    /**指标数量（不包含已删除的） */
    const targetCount = computed(() => tableDataSource.value.length)

    /**
     * 是否显示指定评分人
     *
     * 之所以这样子写，是因为此字段是在绩效对象上携带着
     *  */
    const showAppointScore = computed(() => {
      let pmTargetShowAppointList = (state.dimension?.pmTargetList || []).map(
        item => item.appointScoreSign
      )

      if (pmTargetShowAppointList.includes(1)) {
        return true
      } else {
        return props.showAppointScore
      }
    })

    /**监听传入的维度数据，并重新组合成所需的维度数据 */
    watch(
      () => props.dimension,
      n => {
        if (n) {
          state.dimension = n
          console.log(n, '当前维度:' + n?.pmDimensionName + ',它的绩效列表：', n?.pmTargetList)
        } else {
          state.dimension = undefined
        }
      },
      {immediate: true}
    )

    /**监听组合好的维度数据，有改动就返回给父组件 */
    watch(
      () => state.dimension,
      n => {
        n && emit('update:dimension', n)
        n && emit('dimensionChange', n)
      },
      {deep: true}
    )

    onMounted(() => {})

    return () => (
      <PhoneDimenBlock dimen={props.dimension}>
        <div class='flex flex-col gap-y-12 mt-12'>
          {state.dimension?.pmTargetList.map(target => (
            <PhoneTargetBlock v-model={[target, 'target']}></PhoneTargetBlock>
          ))}
        </div>
      </PhoneDimenBlock>
    )
  }
})

export default ScoreDimen
